package gwtadmin.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FormViewEdit extends SimplePanel implements ClickHandler,AsyncCallback<String> {
	
	public String fileName;
	
	public FormViewEdit(IWsdlManager wsdlManager) {
		
		wsdlMan = wsdlManager;

		textXml.setSize("750px", "350px");
		textXml.setReadOnly(true);
		textXml.getElement().setAttribute("wrap", "off");
		
		btnEdit.setWidth("150px");
		btnDownload.setWidth("150px");
		btnDelete.setWidth("150px");
		
		btnSave.setWidth("150px");
		btnCancel.setWidth("150px");
		
		///////////////////////////////////////////////////////////////
		
		hpView = new HorizontalPanel();
//		hpView.setWidth("100%");
		hpView.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		hpView.getElement().setAttribute("style", "width: 100%; margin: 5px 0px");
		
		hpView.add(btnEdit);
		hpView.add(btnDownload);
		hpView.add(btnDelete);
		
		///////////////////////////////////////////////////////////////
		
		hpEdit = new HorizontalPanel();
//		hpEdit.setWidth("100%");
		hpEdit.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		hpEdit.getElement().setAttribute("style", "width: 100%; margin: 5px 0px");
		
		hpEdit.add(btnSave);
		hpEdit.add(btnCancel);
		
		///////////////////////////////////////////////////////////////

		vpMain.add(textXml);
		vpMain.add(hpView);
		
		addStyleName("undecorFrame");
		
		setWidget(vpMain);
	}
	
	public void backup() {
		xmlBackup = textXml.getText();
	}
	
	public void restore() {
		textXml.setText(xmlBackup);
	}

	@Override
	public void onClick(ClickEvent event) {
		Button btn = (Button)event.getSource();
		
		if (btn == btnDownload) {
			wsdlMan.downloadWsdl(fileName);
		} else if (btn == btnDelete) {
			wsdlMan.removeWsdl(fileName);
		} else if (btn == btnEdit) {
			edit();
		} else if (btn == btnCancel) {
			restore();
			view();
		} else if (btn == btnSave) {
			wsdlMan.saveWsdl(fileName, textXml.getText());
			view();
		}
	}
	
	@Override
	public void onSuccess(String result) {
		setXml(result);
	}
	
	@Override
	public void onFailure(Throwable caught) {
		setXml("Error when editing WSDL");
	}

	public void setXml(String text) {
		textXml.setText(text);
	}
	
	public void setWidth(int width) {
		textXml.setWidth(String.valueOf(width) + "px");
	}
	
	public void showWait(boolean show) {
		if (show) {
			textXml.setText("Wait for loading...");
		} else {
			textXml.setText("");
		}
	}
	
	public void view(String fileName) {
		wsdlMan.getWsdl(fileName, this);
		this.fileName = fileName; 
	}
	
	private void edit() {
		if (editMode)
			return;
		
		backup();
		
		vpMain.remove(hpView);
		vpMain.add(hpEdit);
		
		textXml.setReadOnly(false);
		
		editMode = true;
	}
	
	private void view() {
		if (!editMode)
			return;
		
		vpMain.remove(hpEdit);
		vpMain.add(hpView);
		
		textXml.setReadOnly(true);
		
		editMode = false;
	}

	private boolean editMode = false;
	private TextArea textXml = new TextArea();
	private String xmlBackup = "";
	
	private VerticalPanel vpMain = new VerticalPanel();
	private HorizontalPanel hpView = new HorizontalPanel();
	private HorizontalPanel hpEdit = new HorizontalPanel();
	
	private Button btnEdit = new Button("Edit", this);
	private Button btnDownload = new Button("Download", this);
	private Button btnDelete = new Button("Delete", this);
	
	private Button btnSave = new Button("Save", this);
	private Button btnCancel = new Button("Cancel", this);
	
	private IWsdlManager wsdlMan = null;
}
